package cn.edu.qfnu.oj.entity;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Entity
@DynamicUpdate
public class Classes implements Serializable {

    private Integer Id;
    private Integer classNumber; // 班级号
    private Integer grade;  //年级
    private String classType;  //班级类型

    // 一个班级含有多个学生 班级与学生是一对多关系 （班级中学生不重复）
    private Set<Student> students = new HashSet<>();
    // 一个班级含有多次作业 但多次作业也可以存在于不同班级
    // 班级与作业是多对多关系 （班级中作业不重复）
    private Set<Task> tasks = new HashSet<>();

    public Classes() {}

    public Classes(Integer classNumber, Integer grade, String classType) {
        this.classNumber = classNumber;
        this.grade = grade;
        this.classType = classType;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }


    @Column(name = "class_number")
    public Integer getClassNumber() {
        return classNumber;
    }

    public void setClassNumber(Integer classNumber) {
        this.classNumber = classNumber;
    }

    @Column
    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    @Column(name = "class_type")
    public String getClassType() {
        return classType;
    }

    public void setClassType(String classType) {
        this.classType = classType;
    }

    //班级与学生一对多关系
    @OneToMany(mappedBy = "classFrom")  // 班级与学生一对多关系由学生维护
    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }

    //班级与作业是多对多关系
    @ManyToMany(mappedBy = "classes") // 班级与作业的多对多关系由作业维护
    public Set<Task> getTasks() {
        return tasks;
    }

    public void setTasks(Set<Task> tasks) {
        this.tasks = tasks;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Classes classes = (Classes)o;

        return new EqualsBuilder()
                .append(classNumber, classes.classNumber)
                .append(grade, classes.grade)
                .append(classType, classes.classType)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(classNumber)
                .append(grade)
                .append(classType)
                .toHashCode();
    }


}
